﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Tebbee.UI.Client.AccountSer;
using Tebbee.UI.Client.AdvertiseSer;
using Tebbee.UI.Client.UtilitiesSer;

namespace Tebbee.UI.Client.Controllers.GuestApi
{
    public class AdvDetailsController : ApiController
    {
        private readonly AdvertiseSerClient db = new AdvertiseSerClient();
        private readonly UtilitiesClient ulti = new UtilitiesClient();
        /// <summary>
        /// Function get advertise information
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Advertise_Get_Result GetAdv(int id)
        {
            var tmp = db.GetAdvertise(id);
            var link = String.Empty;
            if (String.IsNullOrEmpty(tmp.Description))
            {
                tmp.Description = "";
            }
            else
            {
                link = tmp.Description.Replace("../", "");
                var path = Path.Combine(HttpRuntime.AppDomainAppPath, link);
                tmp.Description = ulti.ReadFile(path);
            }
            return tmp;
        }

        /// <summary>
        /// Function get all advertise's comments
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IEnumerable<Advertise_GetComment_Result> GetComments(int id)
        {
            return db.Advertise_GetComment(id);
        }

        /// <summary>
        /// Function allow member delete own comment
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HttpResponseMessage DeleteComment(int id)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (db.Advertise_DeleteComment(id) != 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, id);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not Found");
            }
        }

        /// <summary>
        /// Function check if member is logged or not and increase point view advertise if valid
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HttpResponseMessage IsAuthen(int id)
        {
            if (ModelState.IsValid)
            {
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                   
                   bool isAdmin = HttpContext.Current.User.IsInRole("Admin");
                   if (!isAdmin)
                   {
                       var userName = HttpContext.Current.User.Identity.Name;
                       var tmp = db.ViewAdv(userName, id);
                       return Request.CreateResponse(HttpStatusCode.OK, tmp);
                   }
                   else
                   {
                       return Request.CreateResponse(HttpStatusCode.BadRequest);
                   }
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                } 
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        /// <summary>
        /// Parameter request to pass from ajax
        /// </summary>
        public class PostCommentRequest
        {
            public int Id { get; set; }
            public string Comment { get; set; }
        }

        /// <summary>
        /// Function send advertise comment by a member
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public Advertise_SendComment_Result SendComment(PostCommentRequest request)
        {
            Advertise_SendComment_Result tmp = new Advertise_SendComment_Result();
            if (ModelState.IsValid)
            {
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    int id = request.Id;
                    string comment = request.Comment;
                    var userName = HttpContext.Current.User.Identity.Name;

                    tmp = db.Advertise_SendComment(id, userName, comment);
                }
            }
            return tmp;
        }

        public class RateRequest
        {
            public int AdvId { get; set; }
            public double Star { get; set; }
        }

        /// <summary>
        /// Function rate a advertise
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HttpResponseMessage DoRate(RateRequest request)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            else
            {
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    var userName = HttpContext.Current.User.Identity.Name;
                    db.Advertise_Rate(userName, request.AdvId, request.Star );
                    return Request.CreateResponse(HttpStatusCode.OK, request.AdvId);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }
        }

        /// <summary>
        /// Function like or dislike a advertise
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HttpResponseMessage DoLike(int id)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            else
            {
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    var userName = HttpContext.Current.User.Identity.Name;
                    db.Advertise_Like(userName, id);
                    return Request.CreateResponse(HttpStatusCode.OK, id);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }  
        }

        /// <summary>
        /// Function subcribe or unsubcribe a publisher by advertise
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HttpResponseMessage DoSubcribe(int id)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            else
            {
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    var userName = HttpContext.Current.User.Identity.Name;
                    db.Advertise_Subcribe(userName, id);
                    return Request.CreateResponse(HttpStatusCode.OK, id);
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }
        }

        /// <summary>
        /// Function get all advertise's images
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IEnumerable<Advertise_GetImage_Result> GetImages(int id)
        {
            return db.Advertise_GetImage(id);
        }


        public IEnumerable<Advertise_GetMedia_Result> GetMedias(int id)
        {
            var tmp = db.Advertise_GetMedia(id);
            return tmp;
        }





    }
}
